<template>
  <div class="app">
    <div class="box">
      <div class="left"><img src="https://s1.ax1x.com/2022/05/10/Ot9SSK.md.jpg" /></div>
      <div class="right">
        <div class="logo"><img
            src="https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/e08da34488b114bd4c665ba2fa520a31.svg" />
        </div>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm"
          :size="formSize">
          <el-form-item label="账号：" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading">登录</el-button>
            <el-button @click="resetForm(ruleFormRef)">取消</el-button>
          </el-form-item>
        </el-form>
        <div class="link">
          <a href="">回到首页</a>
          <router-link to="/register">注册</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from "vue-router";
import type { ElForm } from 'element-plus'
import { ElMessage } from 'element-plus';
import { MainStore } from '../stroe/index'
type FormInstance = InstanceType<typeof ElForm>

const store = MainStore();
const router = useRouter();
const route = useRoute();
const formSize = ref('')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  username: 'admin',
  password: '123123'
})

const loading = ref(false)

const rules = reactive({
  username: [
    {
      required: true,
      message: '请输入账号',
      trigger: 'blur',
    },
    {
      min: 5,
      max: 12,
      message: 'Length should be 6 to 12',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
    {
      min: 6,
      max: 12,
      message: 'Length should be 6 to 12',
      trigger: 'blur',
    },
  ],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      // const data=await login(ruleForm);
      //开始提交
      loading.value = true;
      const data = await store.login(ruleForm);
      loading.value = false;
      ElMessage.success("登录成功")
      router.push("/admin/home")
    } else {
      ElMessage.error("输入有误请重新输入")
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.box {
  width: 800px;
  height: 500px;
  margin: 100px auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  border-radius: 30px;
}

.left {
  width: 500px;
}

.left img {
  width: 500px;
  height: 500px;
}

.right {
  width: 500px;
  margin-top: 100px;
  margin-right: 20px;
}

.logo {
  margin: 20px 0 40px 70px;
}

.link {
  display: flex;
  justify-content: space-between;
}

.link a {
  margin: 110px 0 0 40px;
}
</style>